package com.yanqu.road.server.cmd.orewar.mall;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.config.goods.BuyGoodsResult;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eMallCategory;
import com.yanqu.road.entity.enums.eShopType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.mall.MallProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityMallModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_ORE_WAR_ACTIVITY_BUY_GOODS,desc = "购买盐场活动道具")
public class OreWarBuyGoodsCmd extends AbstractCommand {

    private int mallId;

    private int count;

    private BuyGoodsResult result;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        MallProto.BuyGoodsReqMsg reqMsg = MallProto.BuyGoodsReqMsg.parseFrom(packet.getBytes());
        mallId = reqMsg.getMallId();
        count = reqMsg.getCount();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (count < 1 || count > GameConfig.MALL_BUY_MAX_COUNT) {
            ret = GameErrorCode.E_MALL_COUNT_NOT_ZERO;
            return;
        }
        MallInfo mallInfo = OreWarActivityMgr.getMallInfo(mallId);
        if (null == mallInfo) {
            ret = GameErrorCode.E_MALL_NO_EXIST;
            return;
        }

        //活动期间购买（活动关联道具）
        if(mallInfo.getCategory() == eMallCategory.InActivityTime.getValue()){
            ActivityInfo activityInfo = OreWarActivityMgr.getOpenActivityInfo(mallInfo.getActivityId());
            if(!ActivityMgr.activityInTime(activityInfo)){
                ret = GameErrorCode.E_MALL_LIMIT_BUY_NO_IN_ACTIVITY_TIME;
                return;
            }
        }

        if(mallInfo.getShopType() != eShopType.OreWarActivityMall.getValue()){
            ret = GameErrorCode.E_MALL_BUY_SHOP_TYPE_ERROR;
            return;
        }

        if(mallInfo.getShopType() == eShopType.OreWarActivityMall.getValue()){
            ActivityInfo activityInfo = OreWarActivityMgr.getOpenActivityInfo(mallInfo.getActivityId());
            if(null == activityInfo){
                ret = GameErrorCode.E_ORE_ACTIVITY_NO_IN_TIME;
                return;
            }
        }

        result = player.getModule(ActivityMallModule.class).buyGoods(mallInfo, count);
    }

    @Override
    public void sendData(GamePlayer player) {
        MallProto.BuyGoodsRespMsg.Builder respMsg = MallProto.BuyGoodsRespMsg.newBuilder();
        respMsg.setRet(ret);
        if(0 == ret){
            if(null != result) {
                respMsg.setRet(result.getRet());
                respMsg.setReward(result.getReward());
            }
        }
        player.sendPacket(Protocol.U_ORE_WAR_ACTIVITY_BUY_GOODS, respMsg);
    }
}
